package com.lgx.right.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.lgx.common.basic.dao.GeneralDao;

import com.lgx.common.exception.DataException;
import com.lgx.common.web.Pagination;
import com.lgx.right.dao.IOrganizationDao;
import com.lgx.right.entity.Organization;

@Component("organizationDao")
public class OrganizationDao extends GeneralDao implements IOrganizationDao {

	protected Logger log = Logger.getLogger(this.getClass());
	
	public Organization createOrganization(Organization org)
			throws DataException {
		this.getHibernateTemplate().save(org);
		log.info("[创建机构]:"+org.getId());
		return org;
	}

	public Organization updateOrganization(Organization org)
			throws DataException {
		this.getHibernateTemplate().update(org);
		log.info("[更新机构]:"+org.getId());
		return org;
	}

	public void deleteOrganization(Organization org)
			throws DataException {
		log.info("[删除机构]:"+org.getId());
		this.getHibernateTemplate().delete(org);
	}

	public void deleteOrganization(long id) throws DataException {
		this.deleteOrganization(this.findOrganizationById(id));
		log.info("[删除机构]:"+id);
	}

	public Organization findOrganizationById(long id) {
		return this.getHibernateTemplate().get(Organization.class, id);
	}
	
	public Organization findOrganizationByUserId(long userId){
		String sql = "from Organization o where o.legal="+userId;
		return (Organization)this.getHibernateTemplate().find(sql).get(0);
	}

	@SuppressWarnings("unchecked")
	public List<Organization> findOrganizationList(String sql) {
		return this.getHibernateTemplate().find(sql);
	}
	
	@SuppressWarnings("unchecked")
	public List<Organization> findOrganizationListByareaNode(String areaNode) {
		String sql = "from Organization o where o.isDelete='0' and o.areaNode = '"+areaNode+"' order by o.id asc";
		return this.getHibernateTemplate().find(sql);
	}

	@SuppressWarnings("unchecked")
	public List<Organization> findOrganizationList(String sql, Object[] params,
			int pageSize, int pageNum) {
		return this.getList(sql, params, pageSize, pageNum);
	}

	public Pagination findOrganizationPagination(String sql, Object[] params,
			int pageSize, int pageNum) {
		return this.getListPagination(sql, params, pageSize, pageNum);
	}
	
	public Pagination findOrganizationPagination(Object[] params, int pageSize,
			int pageNum) {
		String sql = "from Organization o where o.isDelete='0'";
		
		if(params[0] != null)
		{
			sql += " and o.type in ("+params[0]+") ";
		}
		
		if(params[1] != null)
		{
			sql += " and o.name like '%"+params[1]+"%' ";
		}
		
		if(params[2] != null)
		{
			sql += " and o.code like '%"+params[2]+"%' ";
		}
		
		if(params[3] != null)
		{
			sql += " and o.tel like '%"+params[3]+"%' ";
		}
		
		if(params[4] != null)
		{
			sql += " and o.parent.name like  '%"+params[4]+"%' ";
		}
		
		sql += " order by o.id asc";
		
		return this.getListPagination(sql, new Object[]{}, pageSize, pageNum);
	}
	
	@SuppressWarnings("unchecked")
	public List<Organization> findOrganizationParent(String type) {
		String sql = "from Organization o where o.type="+type;
		return this.getHibernateTemplate().find(sql);
	}
	
	
	public void deleteOrganizations(String[] ids) throws DataException {
		for(String id : ids)
		{
			this.deleteOrganization(Long.parseLong(id));
		}
	}

	@SuppressWarnings("unchecked")
	public List<Organization> findOrganizationListByPoints(Double x,Double y){
		String sql = "from Organization o" +
				" where length(o.points)>0" +
				" and sqrt(power(abs("+x+"-substring_index(o.points,',',1)),2)+power(abs("+y+"-substring_index(o.points,',',-1)),2)) <1000" +
				" order by sqrt(power(abs("+x+"-substring_index(o.points,',',1)),2)+power(abs("+y+"-substring_index(o.points,',',-1)),2)) asc";
		return this.getHibernateTemplate().find(sql);
	}
}
